package qf.controller;

import qf.dao.CartDao;
import qf.dao.GoodsDao;
import qf.dao.daoImpl.CartDaoImpl;
import qf.dao.daoImpl.GoodsDaoImpl;
import qf.pojo.Cart;
import qf.pojo.Goods;
import qf.pojo.User;
import qf.service.CartService;
import qf.service.serviceImpl.CartServiceImpl;
import qf.service.serviceImpl.GoodsServiceImpl;
import qf.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 86198
 * @version V1.0
 * @Project Java_qf
 * @Package qf.controller
 * @Description:
 * @Date 2022/5/25 16:05
 */
@WebServlet("/cart")
public class CartController extends BaseServlet{

    public String create(HttpServletRequest request, HttpServletResponse response){
        //获取商品id
        String pid = request.getParameter("pid");

        //获取user的id
        User user = (User)request.getSession().getAttribute("loginUser");

        //判断User对象是不是null空,如果为空重定向到
        if (user == null) {
            return Constants.REDIRECT+"/login.jsp";
        }

        //将User对象id，和商品id放入购物车数据库中，
        CartDao cartDao = new CartDaoImpl();
        CartService cartService = new CartServiceImpl(cartDao);

        cartService.createCart(user.getId(),Integer.parseInt(pid));
        //跳转到购物车查询列表
        return Constants.REDIRECT+"/cartSuccess.jsp";
    }
    public String show(HttpServletRequest request, HttpServletResponse response){
        //获取user的id
        User user = (User)request.getSession().getAttribute("loginUser");

        //判断User对象是不是null空,如果为空重定向到
        if (user == null) {
            return Constants.REDIRECT+"/login.jsp";
        }
        //根据用户id 查询用户的购物车数据
        CartDao cartDao = new CartDaoImpl();
        CartService cartService = new CartServiceImpl(cartDao);
        List<Cart> carts = cartService.selectCartByUid(user.getId());

        //3. 将用户购物车的数据放入request域中, 带到页面中进行展示
        request.setAttribute("list", carts);
        //4. 跳转到购物车列表页面展示
        return "forward:/cart.jsp";
    }
    public String update(HttpServletRequest request, HttpServletResponse response){
        //获取user对象
        User user = (User)request.getSession().getAttribute("loginUser");
        //判断user对象是不是为空 如果为空跳转到登录界面
        if (user == null) {
            return  Constants.REDIRECT+"/login.jsp";
        }
        //获取Cid 购物车id
        String cid = request.getParameter("cid");
        //购车商品数量
        String cnum = request.getParameter("cnum");
        //调用业务层修改
        CartDaoImpl cartDao = new CartDaoImpl();
        CartServiceImpl cartService = new CartServiceImpl(cartDao);
        cartService.update(Integer.parseInt(cid),Integer.parseInt(cnum));

        return  Constants.REDIRECT+"/cart?method=show";

    }

    public String delete(HttpServletRequest request, HttpServletResponse response){
        String cid = request.getParameter("cid");
        User user = (User)request.getSession().getAttribute("loginUser");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        CartDaoImpl cartDao = new CartDaoImpl();
        CartServiceImpl cartService = new CartServiceImpl(cartDao);

        cartService.delect(Integer.parseInt(cid));

        return Constants.REDIRECT+"/cart?method=show";

    }
}
